package com.china08.yxyapi.api.manage.tec.office.cla;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.office.YxyNoticeClass;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.repository.mg.YxyNoticeClassRepository;
import com.china08.yxyapi.util.DateToolUtils;
import com.china08.yxyapi.util.DefaultImageUtil;

@CrossOrigin
@RestController
public class TecOfficeClaListController extends YxyApiBaseController {

    @Autowired
    YxyNoticeClassRepository yxyNoticeClassRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/office/class/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> precess(HttpServletRequest request, HttpServletResponse response, //
            @RequestBody ReqModel reqModel) {

        Integer pageNum = reqModel.page;
        Integer pageSize = reqModel.size;
        String searchType = reqModel.searchType;
        String classId = reqModel.classId;
        List<String> classIdList = reqModel.classIdList;
        String schoolId = getProfile(request, response).getSchoolId();
        String userId = getProfile(request, response).getId();

        Pageable pageable = new PageRequest(pageNum, pageSize, Direction.DESC, "createdDate");
        Page<YxyNoticeClass> page = new PageImpl<>(new ArrayList<>());

        // 全部
        if ("all".equals(searchType)) {
            // 按班级
            if (StringUtils.isNotBlank(classId)) {
                page = yxyNoticeClassRepository.findByClassIdAndSchoolIdOrderByCreatedDateDesc(classId, schoolId, pageable);
            }
            // 教师所教班级
            else {
                page = yxyNoticeClassRepository.findByClassIdInAndSchoolIdOrderByCreatedDateDesc(classIdList, schoolId, pageable);
            }
        }
        // 我的发布
        if ("my".equals(searchType)) {
            // 按班级
            if (StringUtils.isNotBlank(classId)) {
                page = yxyNoticeClassRepository.findByUserIdAndSchoolIdAndClassIdOrderByCreatedDateDesc(userId, schoolId, classId, pageable);
            }
            // 教师所教班级
            else {
                page = yxyNoticeClassRepository.findByUserIdAndSchoolIdOrderByCreatedDateDesc(userId, schoolId, pageable);
            }
        }

        System.out.println(pageNum);
        System.out.println(pageSize);
        System.out.println(searchType);
        System.out.println(classId);
        System.out.println(classIdList);

        List<RespModel> respModelLst = new ArrayList<RespModel>();
        for (YxyNoticeClass yxyNoticeClass : page.getContent()) {
            RespModel respModel = new RespModel();
            respModel.id = yxyNoticeClass.getId();
            String avatar = yxyNoticeClass.getAvatar();
            avatar = DefaultImageUtil.default1(avatar);
            respModel.avatar = avatar;
            respModel.nickName = yxyNoticeClass.getNickName();
            respModel.userId = yxyNoticeClass.getUserId();
            respModel.classNick = yxyNoticeClass.getClassNick();
            respModel.title = yxyNoticeClass.getTitle();
            respModel.content = yxyNoticeClass.getContent();
            respModel.attachmentName = yxyNoticeClass.getAttachmentName();
            respModel.attachmentPath = yxyNoticeClass.getAttachmentPath();
            respModel.date = (null != yxyNoticeClass.getCreatedDate() ? DateToolUtils.format(yxyNoticeClass.getCreatedDate(), "yyyy-MM-dd") : "");
            respModel.readManCount = yxyNoticeClass.getReadManCount(1);
            respModel.readManUserNicknameStr = yxyNoticeClass.getReadManUserNicknameStr(0, "、 ");
            respModel.imageList = yxyNoticeClass.getImage();
            respModelLst.add(respModel);
        }

        Page<RespModel> respModelPage = new PageImpl<RespModel>(respModelLst, pageable, page.getTotalElements());
        return respModelPage;
    }

    public static class ReqModel extends YxyApiPageReqModel {
        public String searchType;
        public String classId;
        // 教师所教的所有班级
        public List<String> classIdList;
    }

    public static class RespModel {
        public String id;
        public String avatar;
        public String nickName;
        public String userId;
        public String classNick;
        public String date;
        public String title;
        public String content;
        public List<String> imageList;
        public String attachmentName;
        public String attachmentPath;
        public long readManCount;
        public String readManUserNicknameStr;
    }

}
